package com.xmb.common.service.impl;

import com.xmb.common.constants.RewardTypeEnum;
import com.xmb.common.entity.XmbUserBalanceBlotter;
import com.xmb.common.entity.XmbUserBonusBlotter;
import com.xmb.common.entity.XmbUserScoreBlotter;
import com.xmb.common.params.AssetsBlotterParam;
import com.xmb.common.service.UserAssetsService;
import com.xmb.common.service.UserBalanceService;
import com.xmb.common.service.UserBonusService;
import com.xmb.common.service.UserScoreService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lyndon on 2019/4/11.
 */
@Service
public class UserAssetsServiceImpl implements UserAssetsService{

    @Resource
    private UserBalanceService balanceService;

    @Resource
    private UserScoreService scoreService;

    @Resource
    private UserBonusService bonusService;

    @Override
    @Transactional
    public int saveAssetsBlotter(AssetsBlotterParam assetsBlotterParam) {
        if (RewardTypeEnum.bonus.name().equals(assetsBlotterParam.getAssetsType())) {
            XmbUserBonusBlotter userBonusBlotter  = new XmbUserBonusBlotter();
            userBonusBlotter.setUserId(assetsBlotterParam.getUserId());
            userBonusBlotter.setExchangeAmount(assetsBlotterParam.getExchangeAmount());
            userBonusBlotter.setTotalAmount(bonusService.getTotalBonus(assetsBlotterParam.getUserId())
                    .add(assetsBlotterParam.getExchangeAmount()));
            userBonusBlotter.setExchangeTitle(assetsBlotterParam.getExchangeTitle());
            userBonusBlotter.setExchangeRemark(assetsBlotterParam.getExchangeRemark());
            userBonusBlotter.setCreator(assetsBlotterParam.getCreator());
            userBonusBlotter.setTypeId(assetsBlotterParam.getTypeId());
            userBonusBlotter.setExchangeDetails(assetsBlotterParam.getExchangeDetails());
            userBonusBlotter.setSupplierId(assetsBlotterParam.getSupplierId());
            return bonusService.saveBonusBlotter(userBonusBlotter);
        } else if (RewardTypeEnum.balance.name().equals(assetsBlotterParam.getAssetsType())) {
            XmbUserBalanceBlotter userBalanceBlotter  = new XmbUserBalanceBlotter();
            userBalanceBlotter.setUserId(assetsBlotterParam.getUserId());
            userBalanceBlotter.setExchangeAmount(assetsBlotterParam.getExchangeAmount());
            userBalanceBlotter.setTotalAmount(balanceService.getTotalBalance(assetsBlotterParam.getUserId())
                    .add(assetsBlotterParam.getExchangeAmount()));
            userBalanceBlotter.setExchangeTitle(assetsBlotterParam.getExchangeTitle());
            userBalanceBlotter.setExchangeRemark(assetsBlotterParam.getExchangeRemark());
            userBalanceBlotter.setCreator(assetsBlotterParam.getCreator());
            userBalanceBlotter.setTypeId(assetsBlotterParam.getTypeId());
            userBalanceBlotter.setExchangeDetails(assetsBlotterParam.getExchangeDetails());
            userBalanceBlotter.setSupplierId(assetsBlotterParam.getSupplierId());
            return balanceService.saveBalanceBlotter(userBalanceBlotter);
        } else if (RewardTypeEnum.score.name().equals(assetsBlotterParam.getAssetsType())) {
            XmbUserScoreBlotter userScoreBlotter  = new XmbUserScoreBlotter();
            userScoreBlotter.setUserId(assetsBlotterParam.getUserId());
            userScoreBlotter.setExchangeAmount(assetsBlotterParam.getExchangeAmount());
            userScoreBlotter.setTotalAmount(scoreService.getTotalScore(assetsBlotterParam.getUserId())
                    .add(assetsBlotterParam.getExchangeAmount()));
            userScoreBlotter.setExchangeTitle(assetsBlotterParam.getExchangeTitle());
            userScoreBlotter.setExchangeRemark(assetsBlotterParam.getExchangeRemark());
            userScoreBlotter.setCreator(assetsBlotterParam.getCreator());
            userScoreBlotter.setTypeId(assetsBlotterParam.getTypeId());
            userScoreBlotter.setExchangeDetails(assetsBlotterParam.getExchangeDetails());
            userScoreBlotter.setSupplierId(assetsBlotterParam.getSupplierId());
            return scoreService.saveScoreBlotter(userScoreBlotter);
        }
        return 0;
    }

    @Override
    @Transactional
    public void saveAssetsBlotterList(List<AssetsBlotterParam> saveAssetsBlotterList) {
        saveAssetsBlotterList.forEach(this::saveAssetsBlotter);
    }
}
